package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.HealthException;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import com.itheima.health.vo.RoleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service(interfaceClass = RoleService.class)
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public PageResult<Role> findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        if(!StringUtils.isEmpty(queryPageBean.getQueryString())){
            // 有查询条件，拼接%
            queryPageBean.setQueryString("%" + queryPageBean.getQueryString() + "%");
        }
        Page<Role> page = roleDao.findByCondition(queryPageBean.getQueryString());
        PageResult<Role> pageResult = new PageResult<>(page.getTotal(), page.getResult());
        return pageResult;
    }

    @Transactional
    @Override
    public void add(RoleVo roleVo) {
        // 添加角色基本信息
        Role role = new Role();
        BeanUtils.copyProperties(roleVo, role);
        roleDao.add(role);
        // 添加成功获取用户id
        Integer roleId = role.getId();

        // 添加角色权限关系
        for (Integer permissionId : roleVo.getPermissionIds()) {
            roleDao.addRolePermissions(permissionId, roleId);
        }

        // 添加角色菜单关系
        for (Integer menuId : roleVo.getMenuIds()) {
            roleDao.addRoleMenus(menuId, roleId);
        }
    }

    @Override
    public Role findById(int roleId) {
        return roleDao.findById(roleId);
    }

    /**
     * 修改角色
     * @param roleVo
     */
    @Transactional
    @Override
    public void update(RoleVo roleVo) {
        // 更新基本信息
        Role role = new Role();
        BeanUtils.copyProperties(roleVo, role);
        roleDao.update(role);

        // 删除原有角色权限
        roleDao.deleteRolePermission(roleVo.getId());

        // 添加新的角色权限
        for (Integer permissionId : roleVo.getPermissionIds()) {
            roleDao.addRolePermissions(permissionId, roleVo.getId());
        }

        // 删除原有角色菜单
        roleDao.deleteRoleMenu(roleVo.getId());

        // 添加新的角色菜单
        for (Integer menuId : roleVo.getMenuIds()) {
            roleDao.addRoleMenus(menuId, roleVo.getId());
        }

    }


    @Transactional
    @Override
    public void deleteById(int roleId) {
        // 检查角色是否被用户引用
        int count = roleDao.findUserByRoleId(roleId);
        if (count > 0) {
            throw new HealthException(MessageConstant.DELETE_ROLE_FAIL);
        }

        // 删除用户所有权限
        roleDao.deleteRoleMenu(roleId);

        // 删除用户所有菜单
        roleDao.deleteRolePermission(roleId);

        // 删除用户基本信息
        roleDao.deleteById(roleId);
    }

    @Override
    public List<Role> findAll() {

        return roleDao.findAll();
    }
}
